package cn.edu.ujn.shi.controller;

import cn.edu.ujn.shi.dao.User;
import cn.edu.ujn.shi.dao.UserType;
import cn.edu.ujn.shi.service.IUserService;
import cn.edu.ujn.shi.service.IUserTypeService;
import jakarta.servlet.RequestDispatcher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;

/**
 * UserController
 *
 * @author clq
 * @version 2024/05/28 21:38
 **/
@Controller
public class UserController {
    @Autowired
    IUserService userService;
    @Autowired
    IUserTypeService iUserTypeService;

    @RequestMapping("/AListUserServlet")
    public String aListUserServlet(HttpServletRequest request, HttpServletResponse response) {
        try {
            response.setContentType("text/html;charset=utf-8");

            List<UserType> list1 = iUserTypeService.getAll();
            HashMap<String, String> userTypeMap = new HashMap<>();
            for (UserType userType : list1) {
                userTypeMap.put(userType.getId() + "", userType.getType());
            }
            String username = request.getParameter("username");
            List<User> list = userService.getUser(username);
            for (User user : list) {
                //判断是否有值，有则转换
                if (userTypeMap.containsKey(user.getType())) {
                    user.setType(userTypeMap.get(user.getType()));
                }
            }
            request.setAttribute("list", list);
            return "AListUserServlet1";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "AListUserServlet1";

    }

    @RequestMapping("/ChangeUserServlet")
    public void changeUserServlet(HttpServletRequest request, HttpServletResponse response) {
        try {
            response.setContentType("text/html;charset=utf-8");
            PrintWriter out = response.getWriter();   //做输出流

            request.setCharacterEncoding("utf-8");

            HttpSession hs = request.getSession();

            //得到用户名
            String username = hs.getAttribute("username").toString();
            //得到前台输入的值
            String userpass = request.getParameter("userpass");
            String truename = request.getParameter("truename");
            String sex = request.getParameter("age");
            String tel = request.getParameter("tel");
            String cardnumber = request.getParameter("cardnumber");
            String type = request.getParameter("type");

            //用户信息维护，更新
            User user = new User(username, userpass, truename, sex, tel, cardnumber, type);
            userService.updateUser(user);
            out.print("<script> alert(\"用户信息更新成功!\"); </script>");
            response.sendRedirect("MainServlet");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/DeleteUserServlet")
    public void deleteUserServlet(HttpServletRequest request, HttpServletResponse response) {
        try {
            response.setContentType("text/html;charset=utf-8");
            request.setCharacterEncoding("utf-8");
            String username = request.getParameter("username");
            userService.delete(username);

            response.sendRedirect("AListUserServlet");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/InsertUserServlet")
    public void insertUserServlet(HttpServletRequest request, HttpServletResponse response) {
        try {
            response.setContentType("text/html;charset=UTF-8");
            PrintWriter out = response.getWriter();   //???????

            request.setCharacterEncoding("utf-8");
            String username = request.getParameter("username");
            String userpass = request.getParameter("userpass");
            String truename = request.getParameter("truename");
            String age = request.getParameter("age");
            String tel = request.getParameter("tel");
            String cardnumber = request.getParameter("cardnumber");
            String type = request.getParameter("type");

            if (username.equalsIgnoreCase("") || userpass.equalsIgnoreCase("")) {
                out.println("<script language = javascript>alert('WARNING')");
                out.println("window.history.go(-1)</script>");
            } else {
                List<User> list = userService.getUser(username);
                try {
                    if (username.equalsIgnoreCase("admin")) {
                        out.println("<script language = javascript>alert('用户重复，请更换用户名!!')");
                        out.println("window.history.go(-1)</script>");
                        return;
                    } else {
                        if (!list.isEmpty()) {
                            out.println("<script language = javascript>alert('用户重复，请更换用户名!')");
                            out.println("window.history.go(-1)</script>");
                        } else {
                            User user = new User(username, userpass, truename, age, tel, cardnumber, type);
                            userService.insert(user);
                            out.println("<script language = javascript>alert('SUCCESS!')");
                            out.println("window.history.go(-1)</script>");
                        }
                    }
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @RequestMapping("/PingServlet")
    public void PingServlet(HttpServletRequest request, HttpServletResponse response) {
        try {
            response.setContentType("text/html;charset=utf-8");
            request.setCharacterEncoding("utf-8");
            PrintWriter out = response.getWriter();

            HttpSession hs = request.getSession();
            String username = hs.getAttribute("username").toString();

            String money = request.getParameter("money");
            String name = request.getParameter("name");
            String people = request.getParameter("people");

            List<User> list = userService.getUser(username);
            String truename = list.get(0).getTruename();

            //引用jsp文件
            RequestDispatcher headerDispatcher = request.getRequestDispatcher("/WEB-INF/jsp/header.jsp");
            headerDispatcher.include(request, response);
            out.println("<center><hr/><img src='images/top6.jpg' width='100%'><hr/></center>");

            out.println("<br/>");
            out.println("<center><img src='images/u5.png' height=70px><font size='+4' face='楷体' color='#7D26CD'><b>评价信息</b></font>");
            out.println("<hr/>");

            out.println("<center>");
            out.println("<form action='InsertDiscussServlet?name=" + name + "&&people=" + people + "&&money=" + money + "' method='post'>");
            out.println("<table>");
            out.println("<tr>");
            out.println("<td>");
            out.println("当前账号：");
            out.println("</td>");
            out.println("<td>");
            out.println(username);
            out.println("</td>");
            out.println("</tr>");

            out.println("<tr>");
            out.println("<td>");
            out.println("失物信息：");
            out.println("</td>");
            out.println("<td>");
            out.println(name);
            out.println("</td>");
            out.println("</tr>");

            out.println("<tr>");
            out.println("<td>");
            out.println("发布账号：");
            out.println("</td>");
            out.println("<td>");
            out.println(people);
            out.println("</td>");
            out.println("</tr>");

            out.println("<tr>");
            out.println("<td>");
            out.println("承诺酬金：");
            out.println("</td>");
            out.println("<td>");
            out.println(money);
            out.println("</td>");
            out.println("</tr>");

            out.println("<tr>");
            out.println("<td>");
            out.println("我的评价:");
            out.println("</td>");
            out.println("<td>");
            out.println("<textarea name='discuss' style='height:200px;width:550px'>");
            out.println("</textarea>");
            out.println("</td>");
            out.println("</tr>");

            out.println("<tr>");
            out.println("<td colspan='2'>");
            out.println("<input type='submit' value='评价' style='width:700px;'/>");
            out.println("</td>");
            out.println("</tr>");
            out.println("</table>");

            out.println("</form>");
            out.println("</center>");

            out.println("<br/><br/><hr/><img src='images/bottom.png' width='100%'>");
            out.println("</body>");
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}